#include <54func.h>
int main(int argc,char *argv[])
{
    // ./1_aqiang_udp 192.168.187.128 1234
    ARGS_CHECK(argc,3);

    int sockfd = socket(AF_INET,SOCK_DGRAM,0);

    //初始化服务器
    struct sockaddr_in serveraddr;
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_port = htons(atoi(argv[2]));
    serveraddr.sin_addr.s_addr = inet_addr(argv[1]);

    //第一次是客户端给服务端发送连接，从而让服务端获取客户端的ip以及端口
    sendto(sockfd,"I am aqiang!",12,0,(struct sockaddr *)&serveraddr,sizeof(serveraddr));
    

    //监听
    char buf[4096] = {0};
    fd_set rdset;
     while(1)
     { 
         FD_ZERO(&rdset);
         FD_SET(STDIN_FILENO,&rdset);
         FD_SET(sockfd,&rdset);
         select(sockfd+1,&rdset,NULL,NULL,NULL);
         //select返回，则说明stdin or sockfd就绪
         if(FD_ISSET(STDIN_FILENO,&rdset))
         { 
             //服务器发送消息给客户端
             bzero(buf,sizeof(buf));
             ssize_t sret = read(STDIN_FILENO,buf,sizeof(buf));
             if(sret == 0)
             {
                 //若是按下ctrl+d，则qiang主动退出
                //此时让aqiang主动给azhen发送一个0字节大小的数据
                sendto(sockfd,buf,0,0,
                       (struct sockaddr *)&serveraddr,sizeof(serveraddr));
                printf("aqiang主动退出！\n");
                break;

             }
             sendto(sockfd,buf,strlen(buf),0,
                           (struct sockaddr *)&serveraddr,sizeof(serveraddr));
             
         } 
         if(FD_ISSET(sockfd,&rdset))
         { 
             //客户端发来消息了
             bzero(buf,sizeof(buf));
             ssize_t sret = recvfrom(sockfd,buf,sizeof(buf),0,NULL,NULL);          
             //实现聊天退出
             if(sret == 0)
             {
                //约定：若是对面发送了一个0字节大小的数据，那么我就退出
                //recvfrom是允许接收0字节，并且返回值为0
                //printf("azhen主动退出！\n");
                 break;
             }
             printf("server：%s\n",buf);
                               
         }
     } 
                 

    return 0;
}


//#include <54func.h>
//int main(int argc, char *argv[])
//{
//    // ./0_client_udp 192.168.72.128 1234
//    ARGS_CHECK(argc,3);
//    int sockfd = socket(AF_INET,SOCK_DGRAM,0);
//    struct sockaddr_in serverAddr;
//    serverAddr.sin_family = AF_INET;
//    serverAddr.sin_port = htons(atoi(argv[2]));
//    serverAddr.sin_addr.s_addr = inet_addr(argv[1]);
//    // 第一次必须是客户端向服务端发消息
//    sendto(sockfd,"hello",5,0,
//           (struct sockaddr *)&serverAddr, sizeof(serverAddr));
//    char buf[4096] = {0};
//    fd_set rdset;
//    while(1){
//        FD_ZERO(&rdset);
//        FD_SET(STDIN_FILENO,&rdset);
//        FD_SET(sockfd,&rdset);
//        select(sockfd+1,&rdset,NULL,NULL,NULL);
//        if(FD_ISSET(sockfd,&rdset)){
//            bzero(buf,4096);
//            ssize_t sret = recvfrom(sockfd,buf,4096,0,NULL,NULL);
//            if(sret == 0){
//                break;
//            }
//            printf("sret = %ld, buf = %s\n", sret, buf);
//        }
//        if(FD_ISSET(STDIN_FILENO,&rdset)){
//            bzero(buf,4096);
//            ssize_t sret = read(STDIN_FILENO,buf,4096);
//            if(sret == 0){
//                sendto(sockfd,buf,0,0,
//                    (struct sockaddr *)&serverAddr,sizeof(serverAddr));
//                break;
//            }
//            sendto(sockfd,buf,strlen(buf),0,
//                   (struct sockaddr *)&serverAddr,sizeof(serverAddr));
//        }
//    }
//
//    return 0;
//}
